﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.administrator.sources;
using VQData;
using VQWEB.common;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for TruongHandler
    /// </summary>
    public class hn_TruongHandler : IHttpHandler,IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            
            #region Truong
            string add_truong = context.Request["add_truong"];
            if (!string.IsNullOrEmpty(add_truong))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_truong>(add_truong);
                if (obj.chuyennganh_array_id != null)
                    bussiness.InsertAllInOne<hn_chuyennganh_truong>(obj.chuyennganh_array_id, "chuyennganh_id", "truong_id", obj.truong_id);
                if (obj.khoi_array_id != null)
                    bussiness.InsertAllInOne<hn_khoi_truong>(obj.khoi_array_id, "khoi_id", "truong_id", obj.truong_id);
                if (obj.hedaotao_array_id != null)
                    bussiness.InsertAllInOne<hn_hedaotao_truong>(obj.hedaotao_array_id, "hedaotao_id", "truong_id", obj.truong_id);
                context.Response.Write("Thực thi thành công !");
            }
            string view_truong = context.Request["view_truong"];
            if (!string.IsNullOrEmpty(view_truong))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderChuyennganhHedaotaoKhoi(context);
            }
            string list_truong = context.Request["list_truong"];
            if (!string.IsNullOrEmpty(list_truong))
            {
                RenderList(context);
            }

            string delete_truong = context.Request["delete_truong"];
            if (!string.IsNullOrEmpty(delete_truong))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.Delete<hn_truong>(delete_truong);
                RenderList(context);
            }

            string lockunlock_truong = context.Request["lockunlock_truong"];
            if (!string.IsNullOrEmpty(lockunlock_truong))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_truong>(lockunlock_truong, "truong_active");
                RenderList(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderList(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_truong>(
                null,
                null,
                null,
                null,
               t => new
               {
                   id = t.truong_id,
                   ma = t.truong_ma,
                   ten = t.truong_ten,
                   logo = t.truong_logo,
                   gioithieu = t.truong_gioithieu,
                   diachi = t.truong_diachi,
                   website = t.truong_website,
                   email=t.truong_email,
                   dienthoai = t.truong_dienthoai,
                   nhomnganh=t.hn_chuyennganh_truongs.Count,
                   khoi=t.hn_khoi_truongs.Count,
                   hedaotao=t.hn_hedaotao_truongs.Count,
                   bosung0 = t.truong_bosung_0,
                   bosung1 = t.truong_bosung_1,
                   bosung2 = t.truong_bosung_2,
                   bosung3 = t.truong_bosung_3,
                   bosung4 = t.truong_bosung_4,
                   bosung5 = t.truong_bosung_5,
                   bosung6 = t.truong_bosung_6,
                   bosung7 = t.truong_bosung_7,
                   bosung8 = t.truong_bosung_8,
                   bosung9 = t.truong_bosung_9,
                   bosung10 = t.truong_bosung_10,
                   bosung11 = t.truong_bosung_11,
                   bosung12 = t.truong_bosung_12,
                   bosung13 = t.truong_bosung_13,
                   bosung14 = t.truong_bosung_14,
                   bosung15 = t.truong_bosung_15,
                   bosung16 = t.truong_bosung_16,
                   bosung17 = t.truong_bosung_17,
                   bosung18 = t.truong_bosung_18,
                   bosung19 = t.truong_bosung_19,
                   bosung20 = t.truong_bosung_20,
                   bosung21 = t.truong_bosung_21,
                   bosung22 = t.truong_bosung_22,
                   bosung23 = t.truong_bosung_23,
                   bosung24 = t.truong_bosung_24,
                   bosung25 = t.truong_bosung_25,
                   bosung26 = t.truong_bosung_26,
                   bosung27 = t.truong_bosung_27,
                   bosung28 = t.truong_bosung_28,
                   bosung29 = t.truong_bosung_29,
                   bosung30 = t.truong_bosung_30,
                   hit = t.truong_hit,
                   ishome = t.truong_ishome,
                   active = t.truong_active
               });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = bussiness.Get<hn_truong>(
               t =>t.truong_id.Equals(Id),
             t => new
             {
                 id = t.truong_id,
                 ma = t.truong_ma,
                 ten = t.truong_ten,
                 logo = t.truong_logo,
                 gioithieu = t.truong_gioithieu,
                 diachi = t.truong_diachi,
                 website = t.truong_website,
                 email = t.truong_email,
                 dienthoai = t.truong_dienthoai,
                 bosung0 = t.truong_bosung_0,
                 bosung1 = t.truong_bosung_1,
                 bosung2 = t.truong_bosung_2,
                 bosung3 = t.truong_bosung_3,
                 bosung4 = t.truong_bosung_4,
                 bosung5 = t.truong_bosung_5,
                 bosung6 = t.truong_bosung_6,
                 bosung7 = t.truong_bosung_7,
                 bosung8 = t.truong_bosung_8,
                 bosung9 = t.truong_bosung_9,
                 bosung10 = t.truong_bosung_10,
                 bosung11 = t.truong_bosung_11,
                 bosung12 = t.truong_bosung_12,
                 bosung13 = t.truong_bosung_13,
                 bosung14 = t.truong_bosung_14,
                 bosung15 = t.truong_bosung_15,
                 bosung16 = t.truong_bosung_16,
                 bosung17 = t.truong_bosung_17,
                 bosung18 = t.truong_bosung_18,
                 bosung19 = t.truong_bosung_19,
                 bosung20 = t.truong_bosung_20,
                 bosung21 = t.truong_bosung_21,
                 bosung22 = t.truong_bosung_22,
                 bosung23 = t.truong_bosung_23,
                 bosung24 = t.truong_bosung_24,
                 bosung25 = t.truong_bosung_25,
                 bosung26 = t.truong_bosung_26,
                 bosung27 = t.truong_bosung_27,
                 bosung28 = t.truong_bosung_28,
                 bosung29 = t.truong_bosung_29,
                 bosung30 = t.truong_bosung_30,
                 hit = t.truong_hit,
                 ishome = t.truong_ishome,
                 active = t.truong_active
             });
            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);
            List<object> array_hedaotao_id = bussiness.List<hn_hedaotao_truong>(
                null,
                null,
                n => n.truong_id.Equals(value),
                null,
                n => n.hedaotao_id
                ).ToList();
            List<object> array_chuyenganh_id = bussiness.List<hn_chuyennganh_truong>(
                null,
                null,
                n => n.truong_id.Equals(value),
                null,
                n => n.chuyennganh_id
                ).ToList();
            List<object> array_khoi_id = bussiness.List<hn_khoi_truong>(
               null,
               null,
               n => n.truong_id.Equals(value),
               null,
               n => n.khoi_id
               ).ToList();
            List<Dictionary<string, object>> listhedaotao = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewHedaotao(), array_hedaotao_id, "id", "ten", "hedaotao_array_id[]");
            List<Dictionary<string, object>> listkhoi = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewKhoi(), array_khoi_id, "id", "ten", "khoi_array_id[]");
            List<Dictionary<string, object>> listchuyennganh = bussiness.RenderArrayMultiSelectJSON(HuongNghiepData.ViewChuyennganh(), array_chuyenganh_id, "id", "ten");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("truong", obj);
            dic.Add("hedaotao", listhedaotao);
            dic.Add("khoi", listkhoi);
            dic.Add("chuyennganh", listchuyennganh);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderChuyennganhHedaotaoKhoi(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> business = new BusinessLayer<HuongNghiepModelDataContext>();
            var chuyennganh = business.RenderArrayMultiSelectJSON(HuongNghiepData.ViewChuyennganh(),null, "id", "ten");
            var hedaotao = business.RenderArrayCheckboxJSON(HuongNghiepData.ViewHedaotao(), null,"id", "ten","hedaotao_array_id[]");
            var khoi = business.RenderArrayCheckboxJSON(HuongNghiepData.ViewKhoi(),null, "id", "ten", "khoi_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("hedaotao", hedaotao);
            dic.Add("chuyennganh", chuyennganh);
            dic.Add("khoi", khoi);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}